﻿using System;
using System.Net;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace TagSearch.Plugins
{
    public class SevenDigital : IClass
    {
        #region JsonParseData

        public class Rootobject
        {
            public Searchresults searchResults { get; set; }
        }

        public class Searchresults
        {
            public List<Searchresult> searchResult { get; set; }
        }

        public class Searchresult
        {
            public Track track { get; set; }
        }

        public class Track
        {
            public int id { get; set; }
            public string title { get; set; }
            public Artist artist { get; set; }
            public int trackNumber { get; set; }
            public Release release { get; set; }
            public DateTime streamingReleaseDate { get; set; }
            public int discNumber { get; set; }
        }

        public class Artist
        {
            public string name { get; set; }
        }

        public class Release
        {
            public string title { get; set; }
            public Artist artist { get; set; }
            public string image { get; set; }
            public DateTime releaseDate { get; set; }
        }

        #endregion
        protected Dictionary<String, int> _TempTotal;
        protected Dictionary<String, List<ITag>> _TempTag;
        private object _Lock = new object();
        public SevenDigital()
        {
            JParse = new System.Text.Json.JsonParser();
            _TempTotal = new Dictionary<string, int>();
            _TempTag = new Dictionary<String, List<ITag>>();
        }
        public override void Search(string Name)
        {
            GetInfo(Name);
        }
        protected override void GetInfo(string Name)
        {
            lock (_Lock)
            {
                _TempTotal.Add(Name, -1);
                _TempTag.Add(Name, new List<ITag>());
            }
            var web = new IWebClient();
            web.DownloadDataCompleted += DownloadDataCompleted;
            web.DownloadDataAsync(new Uri("http://api.7digital.com/1.2/track/search?oauth_consumer_key=YOUR_KEY_HERE&imageSize=500&q=" + Uri.EscapeDataString(Name.ToLower())), new IWebClient.WebClientState(Name, 1, null));

            while (_TempTotal[Name] != _TempTag[Name].Count) { System.Threading.Thread.Sleep(1000); }

            OnEvent(Name, _TempTag[Name]);

            _TempTotal.Remove(Name);
            _TempTag.Remove(Name);
            base.GetInfo(Name);
        }
        protected void DownloadDataCompleted(dynamic sender, dynamic e)
        {
            if (e.Result != null)
            {
                string Name = e.UserState.Name;
                switch ((int)e.UserState.State)
                {
                    case 1:
                        var _RootObject = JParse.Parse<Rootobject>(Encoding.UTF8.GetString(e.Result));
                        _TempTotal[Name] = _RootObject.searchResults.searchResult.Count;
                        foreach (var Json in _RootObject.searchResults.searchResult)
                        {
                            var web = new IWebClient();
                            web.DownloadDataCompleted += DownloadDataCompleted;
                            web.DownloadDataAsync(new Uri(Json.track.release.image), new IWebClient.WebClientState(Name, 2, new ITag(this.GetType(), Json.track.title, Json.track.release.title, Json.track.artist.name, null, Json.track.release.releaseDate, Json.track.discNumber, Json.track.trackNumber)));
                            System.Threading.Thread.Sleep(250);
                        }
                        sender.Dispose();
                        break;
                    case 2: 
                        e.UserState.Tag.Cover = e.Result;
                        _TempTag[Name].Add(e.UserState.Tag);
                        sender.Dispose();
                        break;
                }
            }
        }
    }
}
